package com.zcoj.wx.handler;

import com.zcoj.wx.pojo.WxMsgLog;
import com.zcoj.wx.service.WxMsgLogService;
import com.zcoj.wx.utils.JsonUtils;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author <a href="https://github.com/binarywang">Binary Wang</a>
 */
@Component
public class LogHandler extends AbstractHandler {
    @Autowired
    private WxMsgLogService wxMsgLogService;
    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage,
                                    Map<String, Object> context, WxMpService wxMpService,
                                    WxSessionManager sessionManager) {

        this.logger.info("\n接收到请求消息，内容：\n{}", JsonUtils.toJson(wxMessage));
        Map<String, Object> allFieldsMap = wxMessage.getAllFieldsMap();
        //TODO 将消息保存到本地
        wxMsgLogService.insertWxMsgLog(new WxMsgLog(allFieldsMap));
        return null;
    }

}
